package com.hypertech.core.ffmpeg.service.impl;

import com.hypertech.core.ffmpeg.service.VideoService;
import com.hypertech.core.ffmpeg.videoDao.VideoDao;
import com.hypertech.core.ffmpeg.videoDao.VideoOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoDao videoDao;
    @Value("${ffmpeg.threads}")
    private String threads;
    @Value("${ffmpeg.workSpace}")
    private String workSpace;
    @Override
    public boolean screenshots(String src,String dest,Integer time)throws Exception {
        /*ffmpeg -i src.mp4  -y -f image2 -ss 8 -t 0.001 -s 1920x1080 test.jpg*/
        VideoOptions jpgCommands = new VideoOptions(src,dest);
        jpgCommands.setCmd(workSpace+"ffmpeg.exe");
        jpgCommands.addOption("-f");//格式化
        jpgCommands.addOption("mjpeg");
        jpgCommands.addOption("-ss");//偏移量
        jpgCommands.addOption(time.toString());//截取第几秒的画面
        jpgCommands.addOption("-vframes");
        jpgCommands.addOption("1");//截取1帧
        return videoDao.runCmd(jpgCommands.build());
    }

    @Override
    public boolean low(String src, String dest,String iwm,String twm) throws Exception {
        VideoOptions commands = new VideoOptions(src,dest);
        commands.setCmd(workSpace+"ffmpeg.exe");
        commands.addOption("-vcodec");//编码
        commands.addOption("libx264");
        commands.addOption("-ar");
        commands.addOption("11025");
        commands.addOption("-threads");
        commands.addOption(threads);//线程越多越消耗硬件，转码速度越快
        commands.addOption("-y");//覆盖
        commands.addOption("-maxrate");//-maxrate 500k 最大比特率误差
        commands.addOption("500k");
        commands.addOption("-minrate");
        commands.addOption("500k");
        commands.addOption("-crf");
        commands.addOption("20");
        commands.addOption("-bufsize");
        commands.addOption("2000k");
        //commands.addOption("-profile:v");//-profile:v high 使用H.264的High模式 比較消耗資源,-pre slow 使用慢速模式 耗時間 清晰度高
        //commands.addOption("high");
        //commands.addOption("-aspect");//16:9 / 4:3 視頻比例
        //commands.addOption("16:9");
        //commands.addOption("-b");
        //commands.addOption("0.2M");
        /*音頻
        -acodec libfaac 強制指定音頻處理模式
        -ac 2 聲道選擇
        -ar 44100 音頻赫茲
        -ab 128k 比特率 64k/128k
        -acodec libfaac -ac 2 -ar 44100 -ab 64k*/


        if (!StringUtils.isEmpty(iwm)){
            commands.addOption("-vf");
            commands.addOption(iwm);
            return videoDao.runCmd(commands.build());
        }
        if (!StringUtils.isEmpty(twm)){
            commands.addOption("-vf");
            commands.addOption(twm);
            return videoDao.runCmd(commands.build());

        }
        return videoDao.runCmd(commands.build());
    }

    @Override
    public boolean middle(String src, String dest,String iwm,String twm) throws Exception {
        VideoOptions commands = new VideoOptions(src,dest);
        commands.setCmd(workSpace+"ffmpeg.exe");
        commands.addOption("-vcodec");
        commands.addOption("libx264");
        commands.addOption("-ar");
        commands.addOption("22050");
        commands.addOption("-threads");
        commands.addOption(threads);//线程越多越消耗硬件，转码速度越快
        commands.addOption("-y");
        /*commands.addOption("-preset");
        commands.addOption("ultrafast");
        commands.addOption("-profile:v");
        commands.addOption("baseline");
        commands.addOption("-acodec");
        commands.addOption("aac");
        commands.addOption("-strict");
        commands.addOption("experimental");
        commands.addOption("-q:a");//视频品质
        commands.addOption("6");//视频品质参数
        commands.addOption("-ab");//音频比特率
        commands.addOption("128k");*/
        if (!StringUtils.isEmpty(iwm)){
            commands.addOption("-vf");
            commands.addOption(iwm);
            return videoDao.runCmd(commands.build());
        }
        if (!StringUtils.isEmpty(twm)){
            commands.addOption("-vf");
            commands.addOption(twm);
            return videoDao.runCmd(commands.build());

        }
        return videoDao.runCmd(commands.build());
    }

    @Override
    public boolean high(String src, String dest,String iwm,String twm) throws Exception {
        VideoOptions commands = new VideoOptions(src,dest);
        commands.setCmd(workSpace+"ffmpeg.exe");
        commands.addOption("-vcodec");//编码
        commands.addOption("libx264");
        commands.addOption("-ar");
        commands.addOption("44100");
        commands.addOption("-threads");
        commands.addOption(threads);//线程越多越消耗硬件，转码速度越快
        commands.addOption("-y");//覆盖
        if (!StringUtils.isEmpty(iwm)){
            commands.addOption("-vf");
            commands.addOption(iwm);
            return videoDao.runCmd(commands.build());
        }
        if (!StringUtils.isEmpty(twm)){
            commands.addOption("-vf");
            commands.addOption(twm);
            return videoDao.runCmd(commands.build());

        }
        return videoDao.runCmd(commands.build());
    }

}
